﻿jQuery.noConflict();
(function ($) {
    $.fn.addTab = function (id, title, url, closable, selected) {
        if (!id) return;
        closable = (typeof (closable) == 'undefined') ? true : closable;
        selected = (typeof (selected) == 'undefined') ? true : selected;
        var height = isTouchDevice() ? 'auto' : '100%';
        $('#tabs_container').tabs('add', {
            id: id,
            title: title,
            closable: closable,
            selected: selected,
            style: 'height:' + height + ';',
            content: '<iframe id="tabs_' + id + '_iframe" name="tabs_' + id + '_iframe" allowTransparency= "true"' + (!selected ? (' _src="' + url + '"') : '') + ' src="' + (selected ? url : '') + '"' + (selected ? (' onload="IframeLoaded(\'' + id + '\');"') : '') + ' border="0" frameborder="0" framespacing="0" marginheight="0" marginwidth="0" style="width:100%;height:' + height + ';"></iframe>'
        });
    };
    $.fn.selectTab = function (id) {
        $('#tabs_container').tabs('select', id);
    };
    $.fn.closeTab = function (id) {
        $('#tabs_container').tabs('close', id);
    };
    $.fn.getSelected = function () {
        return $('#tabs_container').tabs('selected');
    };


    //延迟图片加载
    function LoadImage(selector) {
        $(selector).each(function () {
            this.src = this.getAttribute("_src");
            this.removeAttribute("_src");
        });
    }

    function checkActive(id) {
        if ($('#' + id + '_panel:hidden').length > 0)
            $('#' + id).removeClass('active');
        else
            window.setTimeout(checkActive, 300, id);
    };

    function getSecondMenuHTML(id) {
        var html = '';
        for (var i = 0; i < second_array[id].length; i++) {
            var func_id = 'f' + second_array[id][i];
            if (!func_array[func_id])
                continue;

            var func_name = func_array[func_id][0];
            var func_code = func_array[func_id][1];
            var open_window = func_array[func_id][4] ? func_array[func_id][4] : '';
            var bExpand = func_code.substr(0, 1) == "@" && third_array[func_id];
            func_code = func_code.replace("LOGIN_USER_ID", loginUser.user_id)
            var onclick = bExpand ? "" : "createTab(" + func_id.substr(1) + ",'" + func_name.replace("'", "\'") + "','" + func_code.replace("'", "\'") + "','" + open_window + "');";

            html += '<li><a id="' + func_id + '" href="javascript:;" onclick="' + onclick + '"' + (bExpand ? ' class="expand"' : '') + ' hidefocus="hidefocus"><span>' + func_name + '</span></a>';
            if (bExpand) {
                html += '<ul>';
                for (var j = 0; j < third_array[func_id].length; j++) {
                    var func_id1 = 'f' + third_array[func_id][j];
                    var func_name1 = func_array[func_id1][0];
                    var func_code1 = func_array[func_id1][1];
                    var open_window1 = func_array[func_id1][4] ? func_array[func_id1][4] : '';
                    var onclick1 = "createTab(" + func_id1.substr(1) + ",'" + func_name1.replace("'", "\'") + "','" + func_code1.replace("'", "\'") + "','" + open_window1 + "');";
                    html += '<li><a id="' + func_id1 + '" href="javascript:;" onclick="' + onclick1 + '" hidefocus="hidefocus"><span>' + func_name1 + '</span></a></li>';
                }
                html += '</ul>';
            }
            html += '</li>';
        }

        return '<ul id="second_menu">' + html + '</ul>';
    };

    function resizeLayout() {
        // 主操作区域高度
        var wWidth = (window.document.documentElement.clientWidth || window.document.body.clientWidth || window.innerHeight);
        var wHeight = (window.document.documentElement.clientHeight || window.document.body.clientHeight || window.innerHeight);
        var nHeight = $('#north').is(':visible') ? $('#north').outerHeight() : 0;
        var fHeight = $('#funcbar').is(':visible') ? $('#funcbar').outerHeight() : 0;
        var cHeight = wHeight - nHeight - fHeight - $('#south').outerHeight() - $('#hero_bar').outerHeight() - $('#taskbar').outerHeight();
        $('#center').height(cHeight);

        $("#center iframe").css({ height: cHeight });

        /*
              if(isTouchDevice())
              {
                 $('.tabs-panel:visible').height(cHeight);
                 if($('.tabs-panel > iframe:visible').height() > cHeight)
                    $('.tabs-panel:visible').height($('.tabs-panel > iframe:visible').height());
              }
        */
        //一级标签宽度
        var width = wWidth - $('#taskbar_left').outerWidth() - $('#taskbar_right').outerWidth();
        $('#tabs_container').width(width - $('#tabs_left_scroll').outerWidth() - $('#tabs_right_scroll').outerWidth() - 2);
        $('#taskbar_center').width(width - 1);   //-1是为了兼容iPad

        $('#tabs_container').triggerHandler('_resize');
    };

    //菜单滚动箭头事件,id为first_menu
    function initMenuScroll(id) {
        //菜单向上滚动箭头事件
        $('#' + id + ' > .scroll-up:first').hover(
            function () {
                $(this).addClass('scroll-up-hover');
                if (id == 'first_panel') {
                    $("#first_menu > li > a.active").removeClass('active');   //恢复一级active的菜单为正常
                }
            },
            function () {
                $(this).removeClass('scroll-up-hover');
            }
        );

        //点击向上箭头
        $('#' + id + ' > .scroll-up:first').click(
            function () {
                var ul = $('#' + id + ' > ul:first');
                ul.animate({ 'scrollTop': (ul.scrollTop() - SCROLL_HEIGHT) }, 600);
            }
        );

        //向下滚动箭头事件
        $('#' + id + ' > .scroll-down:first').hover(
            function () {
                $(this).addClass('scroll-down-hover');
                if (id == 'first_panel') {
                    $("#first_menu > li > a.active").removeClass('active');   //恢复一级级active的菜单为正常
                }
            },
            function () {
                $(this).removeClass('scroll-down-hover');
            }
        );

        //点击向下箭头
        $('#' + id + ' > .scroll-down:first').click(
            function () {
                var ul = $('#' + id + ' > ul:first');
                ul.animate({ 'scrollTop': (ul.scrollTop() + SCROLL_HEIGHT) }, 600);
            }
        );
    };

    function initStartMenu() {
        //点击页面，隐藏各级菜单面板，并清除二级和三级菜单的active状态
        $('#overlay_startmenu').click(function () {
            if ($('#start_menu_panel:visible').length) {
                $('#overlay_startmenu').hide();
                $('#start_menu_panel').slideUp(300);
                $('#start_menu').removeClass('active');
            }
        });

        //鼠标点击导航图标按钮弹出菜单面板
        $('#start_menu').bind('click', function () {
            if ($('#start_menu_panel:visible').length) {
                $('#overlay_startmenu').hide();
                $('#start_menu_panel').slideUp(300);
                $(this).removeClass('active');
            }

            //延迟加载图片
            LoadImage("#first_menu img[_src]");

            //设置导航图标为active状态
            $(this).addClass('active');

            //遮罩层位置和显示
            $('#overlay_startmenu').show();

            //菜单面板位置
            var top = $('#start_menu').offset().top + $('#start_menu').outerHeight() - 6;
            $('#start_menu_panel').css({ top: top });
            $('#start_menu_panel').slideDown('fast');

            ////计算并设置菜单面板的高度,是否显示滚动箭头
            var scrollHeight = $("#first_menu").attr('scrollHeight');
            if ($("#first_menu").height() < scrollHeight) {
                var height = ($('#south').offset().top - $('#start_menu').offset().top) * 0.7;   //可用高度为开始菜单和状态栏高差的70%
                height = height - height % MENU_ITEM_HEIGHT;   //可用高度为 MENU_ITEM_HEIGHT 的整数倍
                //如果可用高度大于允许的最高高度，则限制
                height = height <= MAX_PNAEL_HEIGHT ? height : MAX_PNAEL_HEIGHT;
                //如果可用高度超过scrollHeight，则设置高度为scrollHeight
                height = height > scrollHeight ? scrollHeight : height;
                $('#first_menu').height(height);
            }
            else {
                var height = scrollHeight > MIN_PNAEL_HEIGHT ? scrollHeight : MIN_PNAEL_HEIGHT;
                $('#first_menu').height(height);
            }

            if ($("#first_menu").height() >= $("#first_menu").attr('scrollHeight')) {
                $('#first_panel > .scroll-up:first').hide();
                $('#first_panel > .scroll-down:first').hide();
            }

            var height = $("#first_panel").height() - $('#portal_panel > .scroll-up:first').height() - $('#portal_panel > .scroll-down:first').height();
            $('#portal_menu').height(height);
            if ($("#portal_menu").height() >= $("#portal_menu").attr('scrollHeight')) {
                $('#portal_panel > .scroll-up:first').hide();
                $('#portal_panel > .scroll-down:first').hide();
            }

            //计算并设置二级菜单面板的位置
            //var top = $('#first_menu').offset().top - $("#start_menu_panel").offset().top;
            //$('#second_panel').css('top', top - 5);
            $('#second_panel > .second-panel-menu').css('height', ($('#first_panel').height() - $('.second-panel-head').outerHeight() - $('.second-panel-foot', second_menu).outerHeight()));

            //第一次打开时设置二级菜单滚动事件
            if ($('#second_panel > .second-panel-menu > .jscroll-c').length <= 0)
                $('#second_panel > .second-panel-menu').jscroll();
        });

        //生成一级菜单
        var html = "";
        for (var i = 0; i < first_array.length; i++) {
            var menu_id = first_array[i];
            if (typeof (func_array['m' + menu_id]) != "object")
                continue;

            var image = !func_array['m' + menu_id][1] ? 'oa' : func_array['m' + menu_id][1];
            html += '<li><a id="m' + menu_id + '" href="javascript:;" hidefocus="hidefocus"><i class="' + image + '"></i> ' + func_array['m' + menu_id][0] + '</a></li>';

            //     html += '<li><a id="m' + menu_id + '" href="javascript:;" hidefocus="hidefocus"><img src="' + static_server + '/static/img/transparent.gif" align="absMiddle" _src="' + static_server + '/static/img/menu/' + image + '.png"/> ' + func_array['m'+menu_id][0] + '</a></li>';
        }
        $("#first_menu").html(html);
        $("#first_menu").mousewheel(function () {
            $('#first_menu').stop().animate({ 'scrollTop': ($('#first_menu').scrollTop() - this.D) }, 300);
        });

        $("#portal_menu").mousewheel(function () {
            $('#portal_menu').stop().animate({ 'scrollTop': ($('#portal_menu').scrollTop() - this.D) }, 300);
        });

        //一级菜单滚动箭头事件
        initMenuScroll('first_panel');

        //门户菜单滚动箭头事件
        initMenuScroll('portal_panel');

        //一级菜单hover和click事件
        $("#first_menu > li > a").click(function () {
            //如果当前一级菜单为active，则返回
            if (this.className.indexOf('active') >= 0)
                return;

            $("#second_menu > li > a.expand").removeClass('active');   //恢复二级expand菜单为正常
            $("#first_menu > li > a.active").removeClass('active');   //恢复一级级active的菜单为正常

            //获取当前一级菜单下属二级菜单的HTML代码，并更新二级菜单面板
            $('#second_panel > .second-panel-menu').html(getSecondMenuHTML(this.id));
            $("#" + this.id).addClass('active');   //将当前一级菜单设为active

            //二级级菜单滚动事件
            $('#second_panel > .second-panel-menu').jscroll();

            //二级菜单点击展开三级菜单
            $('#second_menu > li > a.expand').click(function () {
                $(this).toggleClass('active');
                $(this).parent().children('ul').toggle();
                $('#second_panel > .second-panel-menu').jscroll();
            });
        });

        if (menuExpand != "" && typeof (second_array['m' + menuExpand]) == "object") {
            //展开定义的二级菜单
            $('#m' + menuExpand).addClass('active');
            $('#second_panel > .second-panel-menu').html(getSecondMenuHTML('m' + menuExpand));

            //二级菜单点击展开三级菜单
            $('#second_menu > li > a.expand').click(function () {
                $(this).toggleClass('active');
                $(this).parent().children('ul').toggle();
                $('#second_panel > .second-panel-menu').jscroll();
            });
        }
        else {
            //登录时把常用任务菜单项作为二级菜单的内容
            var html = "";
            for (var i = 0; i < shortcutArray.length; i++) {
                if (typeof (func_array['f' + shortcutArray[i]]) != "object")
                    continue;

                var func_id = 'f' + shortcutArray[i];
                var func_name = func_array[func_id][0];
                var func_code = func_array[func_id][1];
                var open_window = func_array[func_id][4] ? func_array[func_id][4] : "";

                if (func_code.substr(0, 1) == "@")
                    continue;

                var onclick = "createTab(" + func_id.substr(1) + ",'" + func_name.replace("'", "\'") + "','" + func_code.replace("'", "\'") + "','" + open_window + "');";
                html += '<li><a id="' + func_id + '" href="javascript:;" onclick="' + onclick + '" hidefocus="hidefocus"><span>' + func_name + '</span></a></li>';
            }
            html = '<ul id="second_menu">' + html + '</ul>';
            $('#second_panel > .second-panel-menu').html(html);
        }

        $('#second_panel, #second_menu').bind('selectstart', function () {
            return false;
        });

        //在线状态相关事件
        $('#start_menu_panel > .panel-user > .avatar').hover(
            function () { },
            function () { window.setTimeout(function () { $('#on_status').fadeOut(); }, 300); }
        );
        $('#start_menu_panel > .panel-user > .avatar').click(function () {
            $('#on_status').fadeIn(300, function () {
                $(this).css('filter', 'progid:DXImageTransform.Microsoft.shadow(strength=5, direction=135, color=#a3a4a8);');
            });
        });

        $('#on_status > a').click(function () {
            var status = $(this).attr('status');
            if (status < "1" || status > "4") return;

            $.get("ipanel/ispirit_api.php", { API: "on_status", CONTENT: status });
            $('#start_menu_panel > .panel-user > .avatar > .status_icon').attr('class', 'status_icon status_icon_' + status);
            $('#on_status').fadeOut(300);
        });
    }

    function initTabs() {
        //设置标签栏属性
        $('#tabs_container').tabs({
            tabsLeftScroll: 'tabs_left_scroll',
            tabsRightScroll: 'tabs_right_scroll',
            panelsContainer: 'center',
            secondTabsContainer: 'funcbar_left'
        });

        //关闭所有标签后，显示门户切换
        $('#tabs_container').bind('_close', function () {
            if ($('a.tab', this).length <= 0 && $('#start_menu_panel:visible').length <= 0)
                $('#start_menu').trigger('click');
        });
    }

    function initTaskCenter() {
        $('#task_center').bind('click', function () {
            window.setTimeout(function () {
                $().addTab('taskcenter', td_lang.general.project.msg_3, '/general/task_center/', true);
            }, 1);
            //任务中心
        });
    }

    function initIE6Tips() {
        $('#ie6_tips :button.hide-for-ever').click(function () {
            $.cookie('IE6_NO_TIPS', "1", { expires: 1000, path: '/' });
            $('#ie6_tips').hide();
        });

        $('#ie6_tips :button.hide-this-time').click(function () {
            $('#ie6_tips').hide();
        });
    }

    function initTheme() {
        //主题面板显示
        $('#theme').bind('click', function () {
            if (theme_select_priv == 0) {
                alert(theme_select_tip);
                return;
            }
            if ($('#' + this.id + '_panel:visible').length) {
                $('#' + this.id + '_panel').slideUp();
                $('#overlay_theme').hide();
                return;
            }

            if ($('#theme_slider').text() == "") {
                //创建主题图片
                for (var id in themeArray) {
                    if (themeArray[id].src == "") return;
                    var aobj = $('<a class="theme_thumb" hidefocus="hidefocus"><img src="' + themeArray[id].src + '" /><span>' + themeArray[id].title + '</span></a>');
                    aobj.attr("index", id);
                    $('#theme_slider').append(aobj);
                }

                //主题切换
                $('#theme_slider a.theme_thumb').each(function () {
                    var index = $(this).attr("index");
                    if (ostheme == index) {
                        $(this).find("span").addClass("focus");
                    }
                });
                $('#theme_slider a.theme_thumb').live("click", function () {
                    var index = $(this).attr("index");
                    if (ostheme == index) { return; }
                    setTheme(index);
                    $('#theme_slider a.theme_thumb span').removeClass("focus");
                    $(this).find("span").addClass("focus");
                });

                $("#theme_slider a").hover(
                    function () { $(this).addClass("current"); },
                    function () { $(this).removeClass("current"); }
                );

                CheckBkImg('#' + this.id + '_panel div, #' + this.id + '_panel a');
            }

            //面板位置
            $('.over-mask-layer').hide();
            $('#overlay_theme').show();

            var offset = $('#theme').offset();
            var top = offset.top + $('#theme').height() + 5;
            var right = $(document).width() - offset.left - 90 - ($('#theme').width() / 2);
            $('#' + this.id + '_panel').css({ top: top, right: right });
            $('#' + this.id + '_panel').slideDown();

            //常用任务图标设为active状态
            $(this).addClass('active');
            window.setTimeout(checkActive, 300, this.id);

        });

        $('#overlay_theme').click(function () {
            $('#theme').trigger('click');
        });
    }

    function initPersonInfo() {
        //控制面板点击事件
        $('#person_info').bind('click', function () {
            window.setTimeout(function () {
                $().addTab('11', func_array["f11"][0], func_array["f11"][1], func_array["f11"][4]);
            }, 1);   //延迟1毫秒就能从OA精灵窗口打开控制面板标签，奇怪的问题

        });
    }

    function initMark() {
        //云市场
        $('#cloudMark').bind('click', function () {
            window.setTimeout(function () {
                $().addTab('328', func_array["f328"][0], func_array["f328"][1], func_array["f11"][4]);
            }, 1);
        });
    }

    function initSns() {
        //企业社区
        $('#sns').bind('click', function () {
            window.setTimeout(function () {
                $().addTab('73', func_array["f73"][0], func_array["f73"][1], func_array["f11"][4]);
            }, 1);
        });
    }

    function initLogout() {
        //注销
        $('#logout').bind('click', function () {
            logout();
            return false;
        });
    }

    function initHideTopbar() {
        //隐藏topbar事件
        $('#hide_topbar').bind('click', function () {
            $('#north').slideToggle(300, function () { resizeLayout(); });
            $(this).toggleClass('up');

            var hidden = $(this).attr('class').indexOf('up') >= 0;
            $.cookie('hideTopbar', (hidden ? '1' : null), { expires: 1000, path: '/' });
        });

        if ($.cookie('hideTopbar') == '1')
            $('#hide_topbar').triggerHandler('click');
    }

    function initSearch() {
        //搜索框的回车事件
        $('#keyword').keyup(function (event) {
            if (this.value != "" && $('#search_clear:visible').length <= 0)
                $('#search_clear').show();
            else if (this.value == "")
                $('#search_clear').hide();
        });

        $("#keyword").autocomplete({
            source: 'search.php',
            select: function (event, ui) {
                if (ui.item.id != '__SEARCH_HELP__') {
                    //设置延时,处理新打开窗口被原窗口覆盖的情况
                    setTimeout(function () {
                        view_user(ui.item.id)
                    }, 0);
                }
                else {
                    openURL('', (ui.item.label + ' - ' + ui.item.value), '/module/help/search.php?flag=1&keyword=' + ui.item.value)
                }
            }
        });
    }

    function initSmsbox() {
        //点击微讯箱弹出或收起对应面板
        $('#smsbox').click(function () {
            if ($(this).attr('class').indexOf('active') >= 0) {
                window.clearInterval(smsbox_close_countdown);
                window.clearTimeout(smsbox_close_timer);
                $('#smsbox_panel').fadeOut(($.browser.msie && $.browser.version < 11 ? 1 : 300));
                $(this).removeClass('active');
            }
            else {
                $('#org_panel').hide();

                if (ostheme == 13) {
                    $('#smsbox_panel').fadeIn(($.browser.msie && $.browser.version < 11 ? 1 : 600), function () { $(this).css('top', $('#south').offset().top - $(this).height() - 5 + 'px'); });
                }
                else {
                    $('#smsbox_panel').fadeIn(($.browser.msie && $.browser.version < 11 ? 1 : 600), function () { $(this).css('top', $('#south').offset().top - $(this).height() + 3 + 'px'); });
                }
                //$('#smsbox_panel').fadeIn(($.browser.msie && $.browser.version < 11 ? 1 : 600), function(){$(this).css('top', $('#south').offset().top-$(this).height()+3+'px');});
                $(this).addClass('active');
                window.setTimeout(checkActive, 300, this.id);

                if (bSmsPriv)
                    LoadSms();
            }
        });

        //关闭按钮
        var closeBtn = $('#smsbox_panel > .head > .head-center > .head-close');
        closeBtn.hover(
            function () { $(this).addClass('head-close-active'); },
            function () { $(this).removeClass('head-close-active'); }
        );
        closeBtn.click(function () {
            $('#smsbox').triggerHandler('click');
        });

        if (!bSmsPriv)
            return;

        //无新微讯提示
        $('#smsbox_tips').html($('#no_sms').html()).show();
        $('#smsbox_tips').bind('_show', function () {
            window.clearInterval(smsbox_close_countdown);
            window.clearTimeout(smsbox_close_timer);

            smsbox_close_countdown = window.setInterval(function () {
                var seconds = parseInt($('#smsbox_close_countdown').text());
                if (seconds > 0) {
                    $('#smsbox_close_countdown').text(seconds - 1);
                }
                else {
                    window.clearInterval(smsbox_close_countdown);
                    $('#smsbox_close_countdown').text(smsbox_close_timeout);
                }
            }, 1000);

            smsbox_close_timer = window.setTimeout(function () {
                if ($('#smsbox_tips:visible').length > 0)
                    $('#smsbox').triggerHandler('click');
            }, smsbox_close_timeout * 1000);
        });

        //滚动按钮
        $('#smsbox_scroll_up,#smsbox_scroll_down').hover(
            function () { $(this).addClass('active'); },
            function () { $(this).removeClass('active'); }
        );
        $('#smsbox_scroll_up').click(function () {
            var listContainer = $('#smsbox_list_container');
            var blockHeight = $('div.list-block:first', listContainer).outerHeight();
            listContainer.animate({ scrollTop: listContainer.scrollTop() - blockHeight * 3 }, 300);
        });
        $('#smsbox_scroll_down').click(function () {
            var listContainer = $('#smsbox_list_container');
            var blockHeight = $('div.list-block:first', listContainer).outerHeight();
            listContainer.animate({ scrollTop: listContainer.scrollTop() + blockHeight * 3 }, 300);
        });

        //列表鼠标滚轮事件
        $("#smsbox_list_container").mousewheel(function () {
            //$('#smsbox_list_container').scrollTop($('#smsbox_list_container').scrollTop() - this.D);
            $('#smsbox_list_container').stop().animate({ 'scrollTop': ($('#smsbox_list_container').scrollTop() - this.D) }, 300);
        });

        //列表内容变化
        $('#smsbox_list_container').bind('_change', function () {
            $('#smsbox_scroll_up,#smsbox_scroll_down').toggle($(this).outerHeight() < $(this).attr('scrollHeight'));
            if (newSmsArray.length > 0) {
                $('#smsbox_tips').hide();
                $('#no_sms').hide()
                $('#smsbox_list_container > div.list-block:first').trigger('click');
            }
            else {
                $('#smsbox_tips').html($('#no_sms').html()).show(0, function () { $(this).triggerHandler('_show'); });
            }
        });

        //列表hover效果
        var listBlocks = $('#smsbox_list_container > div.list-block');
        listBlocks.live('mouseenter', function () { $(this).addClass('list-block-hover'); });
        listBlocks.live('mouseleave', function () { $(this).removeClass('list-block-hover'); });

        //列表click事件
        listBlocks.live('click', function () {
            if ($(this).attr('class').indexOf('list-block-active') > 0) return;
            $('#smsbox_list_container > div.list-block').removeClass('list-block-active');
            $(this).addClass('list-block-active');
            $('table', this).removeClass('unread');

            var id = $(this).attr('group_id');
            var html = '';
            selectedRecvSmsIdStr = selectedSendSmsIdStr = '';
            for (var i = 0; i < newSmsArray.length; i++) {
                if (!((newSmsArray[i].from_id == id && newSmsArray[i].to_id == loginUser.uid) || (newSmsArray[i].from_id == loginUser.uid && newSmsArray[i].to_id == id)))
                    continue;

                //接收的微讯
                if (newSmsArray[i].receive)
                    selectedRecvSmsIdStr += newSmsArray[i].sms_id + ',';
                else
                    selectedSendSmsIdStr += newSmsArray[i].sms_id + ',';

                newSmsArray[i].unread = 0;
                var name = newSmsArray[i].from_name;
                var time = newSmsArray[i].send_time.indexOf(' ') > 0 ? newSmsArray[i].send_time : newSmsArray[i].send_time;
                var from_type_name = newSmsArray[i].from_type_name;

                html += CreateMsgBlock({ "sms_id": newSmsArray[i].sms_id, "class": (newSmsArray[i].receive ? "from" : "to"), "user": newSmsArray[i].from_id, "type_id": newSmsArray[i].type_id, "from_type_name": from_type_name, "name": name, "time": time, "content": newSmsArray[i].content, "url": newSmsArray[i].url });
            }


            $('#smsbox_msg_container').html(html);
            window.setTimeout(function () {
                $('#smsbox_msg_container').animate({ scrollTop: $('#smsbox_msg_container').attr('scrollHeight') }, 300);
            }, 1);   //延迟1毫秒后取出的scrollHeight就是正确的，奇怪的问题

        });

        //全部已阅
        $('#smsbox_read_all').click(function () {
            var array = GetSmsIds();
            RemoveSms(array.recv, array.send, 0);
        });

        //全部删除
        $('#smsbox_delete_all').click(function () {
            var array = GetSmsIds();
            RemoveSms(array.recv, array.send, 1);
        });

        //全部详情
        $('#smsbox_detail_all').click(function () {
            var sms_id_str = '';
            var sms_id_str0 = '';
            var nav_mail_url = '';
            for (var i = 0; i < newSmsArray.length; i++) {
                if (newSmsArray[i].receive != '1')
                    continue;

                nav_mail_url = newSmsArray[i].url;
                sms_id_str += newSmsArray[i].sms_id + ',';
                if (newSmsArray[i].type_id == '0')
                    sms_id_str0 += newSmsArray[i].sms_id + ',';
            }
            newSmsArray = [];
            FormatSms();
            jQuery('#smsbox_msg_container').html('');
            openURL('', '', '/module/nav/?SMS_ID_STR=' + sms_id_str + '&SMS_ID_STR0=' + sms_id_str0 + '&NAV_MAIN_URL=' + encodeURIComponent(nav_mail_url), '1');
            selectedRecvSmsIdStr = selectedSendSmsIdStr = '';
        });


        //已阅
        $('#smsbox_toolbar_read').click(function () {
            var lis = $('#smsbox_list_container > div.list-block');
            var acliindex = lis.index($('div.list-block-active'));
            RemoveSms(selectedRecvSmsIdStr, selectedSendSmsIdStr, 0, acliindex);
        });

        //删除
        $('#smsbox_toolbar_delete').click(function () {
            var lis = $('#smsbox_list_container > div.list-block');
            var acliindex = lis.index($('div.list-block-active'));
            RemoveSms(selectedRecvSmsIdStr, selectedSendSmsIdStr, 1, acliindex);
        });

        //内容块hover效果
        var msgBlocks = $('#smsbox_msg_container > div.msg-block');
        msgBlocks.live('mouseenter', function () { $(this).addClass('msg-hover'); });
        msgBlocks.live('mouseleave', function () { $(this).removeClass('msg-hover'); });

        //内容块click事件
        msgBlocks.live('click', function () {
            $('#smsbox_msg_container > div.msg-block').removeClass('msg-active');
            $(this).addClass('msg-active');
        });

        //回复事件
        $('.head > .operation > a.reply', msgBlocks).live('click', function () {
            $('#smsbox_textarea').trigger('focus');
        });

        //查看详情事件
        $('.head > .operation > a.detail', msgBlocks).live('click', function () {
            var sms_id = $(this).attr('sms_id');
            var url = $(this).attr('url');
            RemoveSms(sms_id, '', 0);
            openURL('', '', url, '1');
        });

        //快速回复
        $('#smsbox_rapid_reply').change(function () {
            if (this.selectedIndex == 0)
                return;
            $('#smsbox_textarea').val(this.options[this.selectedIndex].text)
        });

        //输入框Ctrl + Enter事件
        $('#smsbox_textarea').keypress(function (event) {
            if (event.keyCode == 10 || event.ctrlKey && event.which == 13)
                $('#smsbox_send_msg').triggerHandler('click');
        });

        //发送
        $('#smsbox_textarea').bind('focus', function () {
            $(this.parentNode).addClass('center-reply-focus');
        });
        $('#smsbox_textarea').bind('blur', function () {
            $(this.parentNode).removeClass('center-reply-focus');
        });

        $('#smsbox_send_msg').click(function () {
            var msg = $('#smsbox_textarea').val();
            if (!msg) return;

            var user_id = $('#smsbox_msg_container > div.msg-active:first').attr('user') || $('#smsbox_list_container > div.list-block-active:first').attr('user');
            if (!user_id) {
                alert(td_lang.inc.msg_22);//'请选择发送用户'
                return;
            }

            var reg = /\n/g;
            var content = msg.replace(reg, "<br />");
            var date = new Date();
            var html = CreateMsgBlock({ "sms_id": "send_" + (maxSendSmsId++), "class": "to", "user": loginUser.uid, "name": loginUser.user_name, "time": date.toTimeString().substr(0, 5), "content": content });
            $('#smsbox_msg_container').append(html);
            window.setTimeout(function () {
                $('#smsbox_msg_container').animate({ scrollTop: $('#smsbox_msg_container').attr('scrollHeight') }, 300);
            }, 1);

            newSmsArray[newSmsArray.length] = { sms_id: "", to_id: user_id, from_id: loginUser.uid, from_name: loginUser.user_name, type_id: "0", type_name: "个人微讯", send_time: date.toTimeString().substr(0, 5), unread: 0, content: content, url: "", receive: 0 };
            //newSmsArray = $.merge(array, newSmsArray);

            jQuery.ajax({
                type: 'POST',
                url: 'status_bar/msg_send.php',
                data: { 'TO_UID': user_id, 'CONTENT': msg, 'charset': 'utf-8' },
                dataType: 'text',
                success: function (data) {
                    $('#smsbox_textarea').val("");
                },
                error: function (request, textStatus, errorThrown) {
                    var msg1 = sprintf(td_lang.inc.msg_108, textStatus);
                    alert(msg1);
                }
            });

            $('#smsbox_textarea').trigger('focus');
        });
    }

    //lp 事务提醒初始化事件
    function initNocbox() {

        //绑定打开事务提醒
        var openBtn = $('#nocbox');
        openBtn.click(function () {
            var nocboxPanel = $('#new_noc_panel').css("display");
            nocboxPanel == "none" ? noc_mon() : CloseNoc();
        });

        //按钮绑定关闭事件
        var closeBtn = $('#new_noc_panel > #new_noc_title > span.noc_iterm_close');
        closeBtn.click(function () {
            CloseNoc();
        });

        //内容块hover效果
        var nocBlocks = $('#new_noc_list > div.noc_iterm > ul.noc_iterm_data > li');
        nocBlocks.live('mouseenter', function () { $(this).addClass('noc_iterm_hover'); });
        nocBlocks.live('mouseleave', function () { $(this).removeClass('noc_iterm_hover'); });

        //点击查看提醒
        $('#new_noc_list > div.noc_iterm > ul.noc_iterm_data > li').live('click', function () {
            var url = $(this).attr('url');
            var sms_id = $(this).attr('sms_id');
            var type_id = $(this).attr('type_id');
            RemoveNoc($(this), sms_id, 0);
            if (url != "") {
                openURL('', '', url, '1');
            }
        });

        $('#new_noc_list > div.noc_iterm > ul.noc_iterm_data > li > a').click(function (e) {
            e.stopPropagation();
        })

        //点击查看历史记录
        $('#check_remind_histroy').live('click', function () {
            openURL('', td_lang.inc.msg_23, "sms/remind_center");   //'事务提醒历史记录'
        });

        //点击全部已阅
        $('#ViewAllNoc').live('click', function () {
            var idstr = get_noc_idstr();
            jQuery.ajax({
                type: 'POST',
                url: 'status_bar/sms_submit.php',
                data: { 'SMS_ID': idstr },
                cache: false,
                success: function () {
                    jQuery('#new_noc_list').empty();
                    var datanum = get_noc_num();
                    jQuery("#new_noc_panel > #new_noc_title > .noc_iterm_num >span").html(datanum);
                    jQuery("#new_noc_panel > .button").find("#ViewAllNoc,#ViewDetail").hide();
                    CloseNoc();
                },
                error: function (request, textStatus, errorThrown) {
                    jQuery('#new_noc_list').hide();
                    jQuery('#nocbox_tips').html('<div class="error">' + td_lang.inc.msg_24 + '(' + request.status + ')：' + textStatus + '</div>').show();//操作失败
                }
            });
        });

        //点击全部详情
        $('#ViewDetail').live('click', function () {
            var idstr = firsturl = separator = "";
            var idobj = $('#new_noc_list > .noc_iterm > .noc_iterm_data > li');
            var readobj = $('#new_noc_list > div.noc_iterm > .noc_iterm_title > .noc_iterm_read');
            var idstr_all = get_noc_idstr();
            idobj.each(function () {
                url = $(this).attr("url");
                sms_id = $(this).attr("sms_id");
                if (url != "" && firsturl == "") {
                    firsturl = url;
                }
                if (url != "") {
                    idstr += separator + jQuery(this).attr("sms_id");
                    separator = ",";
                }
            });

            window.open('/module/nav/?SMS_ID_STR=' + idstr + '&NAV_MAIN_URL=' + encodeURIComponent(firsturl));
            RemoveNoc(readobj, idstr_all, 0);
            CloseNoc();
        });

        $('#CloseBtn').live('click', function () {
            CloseNoc();
        });

        //点击类型，类型全部详情
        $('#new_noc_list > div.noc_iterm > .noc_iterm_title > .noc_iterm_read').live('click', function () {
            var idstr = idstr_all = firsturl = "";
            var separator = ",";
            var type_id = $(this).attr('type_id');
            var idstr_all = get_noc_idstr(type_id);
            RemoveNoc($(this), idstr_all, 0);
            var idobj = jQuery("#new_noc_panel > #new_noc_list > .noc_iterm_" + type_id + " > .noc_iterm_data > li");
            idobj.each(function () {
                url = $(this).attr("url");

                if (url == "") {
                    return true;
                }

                sms_id = $(this).attr("sms_id");
                if (url != "" && firsturl == "") {
                    firsturl = url;
                }
                if (url != "") {
                    idstr += separator + jQuery(this).attr("sms_id");
                    separator = ",";
                }
            });
            url = '/module/nav/?SMS_ID_STR=' + idstr + '&NAV_MAIN_URL=' + encodeURIComponent(firsturl);
            if (idstr != "") {
                openURL('', '', url, '1');
            }
        });

        //点击类型，类型全部已阅
        $('#new_noc_list > div.noc_iterm > .noc_iterm_title > .noc_iterm_cancel').live('click', function () {
            var type_id = $(this).attr('type_id');
            var idstr_all = get_noc_idstr(type_id);
            RemoveNoc($(this), idstr_all, 0);
        });

    };

    function initOrg() {
        //组织面板和在线人员的显示和隐藏事件
        $('#org_panel').bind('_show', function () {
            if ($('#user_online:visible').length > 0) {
                $('#user_online').triggerHandler('_show');
                $('#user_all').triggerHandler('_hide');
            }
            else {
                $('#user_online').triggerHandler('_hide');
                $('#user_all').triggerHandler('_show');
            }
        });
        $('#org_panel').bind('_hide', function () {
            if (timer_online_tree_ref)
                window.clearInterval(timer_online_tree_ref);
            if ($('#user_online:hidden').length > 0)
                $('#user_online').triggerHandler('_hide');
        });

        //点击组织弹出或收起对应面板
        $('#org').click(function () {
            if ($(this).attr('class').indexOf('active') >= 0) {
                $('#org_panel').fadeOut(($.browser.msie && $.browser.version < 11 ? 1 : 300));
                $(this).removeClass('active');
                $('#org_panel').triggerHandler('_hide');
            }
            else {
                if ($('#org_panel').text() == "") {
                    var html = '<div class="head">';
                    html += '<div class="left"><a href="javascript:;" onClick="ActiveUserTab(this)" id="user_online_tab" class="active" hidefocus="hidefocus"><span><img src="' + static_server + '/static/img/org/U01.png" align="absMiddle"> ' + td_lang.inc.msg_128 + '</span></a></div>';
                    html += '<div class="right"><a href="javascript:;" onClick="ActiveUserTab(this)" hidefocus="hidefocus"><span><img src="' + static_server + '/static/img/org/group.png" align="absMiddle"> ' + td_lang.inc.msg_129 + '</span></a></div>';
                    html += '</div>';
                    html += '<div class="center">';
                    html += '<div class="top">';
                    html += '<div id="user_online"><div id="orgTree0" style="height:320px;"></div></div>';
                    html += '<div id="user_all" style="display:none;"><div id="orgTree1" style="height:320px;"></div></div>';
                    html += '</div>';
                    html += '<div class="bottom">';
                    html += '<a class="btn-white-b" href="javascript:;" onClick="SearchUser();" hidefocus="hidefocus">' + td_lang.inc.msg_41 + '</a>&nbsp;&nbsp;';
                    html += '<a class="btn-white-b" href="javascript:;" onClick="jQuery(\'#org\').click();" hidefocus="hidefocus">' + td_lang.global.close + '</a>';
                    html += '</div>';
                    html += '</div>';
                    html += '<div class="foot">';
                    html += '<div class="left"></div>';
                    html += '<div class="right"></div>';
                    html += '</div>';
                    html += '<div class="corner"></div>';
                    $('#org_panel').html(html);

                    //在线人员
                    $('#user_online').bind('_show', function () {
                        if (timer_online_tree_ref)
                            window.clearInterval(timer_online_tree_ref);

                        if ($("#orgTree0").html() == "") {
                            $("#orgTree0").html('<iframe id="orgTree0_iframe" allowTransparency= "true" src="" border="0" frameborder="0" framespacing="0" marginheight="0" marginwidth="0" style="width:100%;height:98%;"></iframe>');
                            $("#orgTree0 iframe").attr('src', ("/inc/user_tree.php?TREE_ID=orgTree0&SHOW_IP=" + show_ip + "&SHOW_BUTTON=" + show_button + "&JSON_URL=" + jsonURL0));
                        }
                        else if ((new Date()).getTime() - timeLastLoadOnline > monInterval.online * 5 * 1000) {
                            frames['orgTree0_iframe'].tree.reload();
                        }

                        timer_online_tree_ref = window.setInterval(function () { frames['orgTree0_iframe'].tree.reload(); }, monInterval.online * 5 * 1000);
                        timeLastLoadOnline = (new Date()).getTime();
                    });
                    $('#user_online').bind('_hide', function () {
                        if (timer_online_tree_ref)
                            window.clearInterval(timer_online_tree_ref);
                    });

                    //全部人员第一次显示加载全部人员树
                    $('#user_all').bind('_show', function () {
                        if ($("#orgTree1").html() == "") {
                            $("#orgTree1").html('<iframe id="orgTree1_iframe" allowTransparency= "true" src="" border="0" frameborder="0" framespacing="0" marginheight="0" marginwidth="0" style="width:100%;height:98%;"></iframe>');
                            $("#orgTree1 iframe").attr('src', ("/inc/user_tree.php?TREE_ID=orgTree1&SHOW_IP=" + show_ip + "&SHOW_BUTTON=" + show_button + "&JSON_URL=" + jsonURL1));
                        }
                    });

                    CheckBkImg('#' + this.id + '_panel div, #' + this.id + '_panel a');
                }

                $('#smsbox_panel').hide();
                if (ostheme == 13) {
                    $('#org_panel').fadeIn(($.browser.msie && $.browser.version < 11 ? 1 : 600), function () { $(this).css('top', $('#south').offset().top - $(this).outerHeight() - 6 + 'px'); $('#org_panel').triggerHandler('_show'); });
                }
                else {
                    $('#org_panel').fadeIn(($.browser.msie && $.browser.version < 11 ? 1 : 600), function () { $(this).css('top', $('#south').offset().top - $(this).outerHeight() + 3 + 'px'); $('#org_panel').triggerHandler('_show'); });
                }
                $('#org_panel').fadeIn(($.browser.msie && $.browser.version < 11 ? 1 : 600), function () { $(this).css('top', $('#south').offset().top - $(this).outerHeight() + 3 + 'px'); $('#org_panel').triggerHandler('_show'); });
                $(this).addClass('active');
                window.setTimeout(checkActive, 300, this.id);
            }
        });
    }

    function initDatetime() {
        $('#date,#mdate').click(function () {
            $().addTab('dt_date', td_lang.inc.msg_25, "/module/calendar2/", true);//"万年历"
        });
        $('#time_area').click(function () {
            $().addTab('dt_time_area', td_lang.inc.msg_26, "/module/world_time/", true);//"世界时间"
        });
    }

    function initHeroBar() {
        if ($('#tongdainfo').length <= 0)
            return;

        $.get('tongda.php', function (data) {
            if (data.code == '1' && typeof (data.data) == 'object' && data.data.length > 0) {
                $('#tongdainfo a').remove();
                for (var i = 0; i < data.data.length; i++) {
                    if (data.data[i].type == '1') {
                        if (jQuery.cookie('hide_notice_' + data.data[i].id) != '1' && (data.data[i].flag != '1' || cur_login_user_priv == '1')) {
                            jQuery('#notice_tip').show();
                            jQuery('#notice_head').html(data.data[i].text);
                            jQuery('#notice_body').html(data.data[i].content);
                            jQuery('#notice_foot').html('<a onclick="jQuery(\'#notice_tip\').hide();jQuery.cookie(\'hide_notice_' + data.data[i].id + '\', 1, {expires:1000, path:\'/\'});" href="' + data.data[i].href + '" class="btn-white-b" target="_blank">' + td_lang.inc.msg_146 + '</a>&nbsp;<a href="javascript:;" class="btn-white-b" onclick="jQuery(\'#notice_tip\').hide();jQuery.cookie(\'hide_notice_' + data.data[i].id + '\', 1, {expires:1000, path:\'/\'});">' + td_lang.global.close + '</a>');
                        }
                    }
                    else {
                        $('#tongdainfo').append('<a href="' + data.data[i].href + '" class="' + data.data[i].class_name + '" target="' + data.data[i].target + '" style="' + data.data[i].style + '"><span>' + data.data[i].text + '</span></a>');
                    }
                }
            }
        }, 'json');
    }

    //窗口resize事件
    //if(!isTouchDevice())
    $(window).resize(function () { resizeLayout(); });
    //else
    //   $(window).bind('orientationchange', function(){resizeLayout();});

    $(document).ready(function ($) {
        $('#loading').remove();

        //iPad 等触屏设备
        if (isTouchDevice()) {
            $('body').addClass('mobile-body');
            $('#center').addClass('mobile-center');
        }

        //调整窗口大小
        resizeLayout();

        //开始菜单
        initStartMenu();

        //标签栏
        initTabs();

        //任务中心
        initTaskCenter();

        //主题切换
        initTheme();

        //控制面板事件
        initPersonInfo();

        //注销事件
        initLogout();

        //打开云市场
        initMark();

        //打开企业社区
        initSns();

        //隐藏topbar事件
        initHideTopbar();

        //搜索框
        initSearch();

        //IE6提示栏
        initIE6Tips();

        //组织面板和在线人员
        initOrg();

        //微讯箱
        initSmsbox();

        //事务提醒
        initNocbox();

        //加载门户
        for (var i = 0; i < portalLoadArray.length; i++) {
            $().addTab('portal_' + portalLoadArray[i].id, portalLoadArray[i].title, portalLoadArray[i].url, portalLoadArray[i].closable, (i == 0));
        }

        //日期时间和天气
        mdate();
        timeview();
        if (bInitWeather) {
            InitProvince(weatherCity);
            if ($('#weather').text() == "")
                $('#weather').html(sprintf('<a href="javascript:GetWeather();">%s</a>', td_lang.inc.msg_141));
        }
        initDatetime();

        //新微讯、在线人员监控和状态栏文字滚动
        timer_sms_mon = window.setTimeout(sms_mon, 3000);
        window.setTimeout(online_mon, monInterval.online * 1000);
        window.setInterval(StatusTextScroll, statusTextScroll * 1000);

        //紧急通知相关事件
        if ($('#notify_panel').length > 0) {
            $('#overlay').show();
            $('#notify_panel').show();
            $('#notify_panel').css('left', ($(window).width() - $('#notify_panel').width()) / 2);

            $('#notify_panel .btn-ok').click(function () {
                $.cookie($(this).attr("cookie_name"), $(this).attr("cookie_value"), { expires: 1000, path: '/' });
                $('#notify_panel .btn-close').click();
            });
            $('#notify_panel .btn-close').click(function () {
                $('#overlay').hide();
                $('#notify_panel').hide();
            });
            $('#notify_panel .head-close').click(function () {
                $('#notify_panel .btn-close').click();
            });
        }

        CheckBkImg('div,a,ul,li,span');

        initHeroBar();

        //工作台代码
        function hiddenWorkBench() {
            jQuery('#my_workbench .list-wrap').css('display', 'none')
            jQuery('#overlay_workbench').css('display', 'none')
        }
        //解决ie下window.location.origin的兼容性问题
        if (window["context"] == undefined) {
            if (!window.location.origin) {
                window.location.origin = window.location.protocol + "//" + window.location.hostname +
                    (window.location.port ? ':' + window.location.port : '');
            }
            window["context"] = location.origin + "/V6.0";
        }
        function skipWorkbench(item) {
            var name = item.getAttribute("name");
            var url = window.location.origin + '/portal/views/handle/index.php?#/workbench/' + item.id
            createTab && createTab('my-workbench' + item.id, name, url, '');
            hiddenWorkBench()
        }
        // var params = {
        //     action: 'GetWorkbenchList'
        // }
        jQuery('#my_workbench .workbench-set').click(function (e) {
            var url = window.location.origin + '/portal/views/workbench/'
            createTab && createTab('workbench', '工作台管理', url, '');
            e.stopPropagation();
            hiddenWorkBench()
        })
        jQuery('#overlay_workbench').click(function (e) {
            e.stopPropagation();
            hiddenWorkBench()
        })
        jQuery('#my_workbench').click(function () {
            jQuery.ajax({
                url: '/general/appbuilder/web/portal/workbench/getworklist',
                type: 'get',
                // data: params,
                success: function (myData, textStatus, jqXHR) {
                    // function isJSON(str) {
                    //     if (typeof str == 'string') {
                    //         try {
                    //             JSON.parse(str);
                    //             return true;
                    //         } catch (e) {
                    //             console.log(e);
                    //             return false;
                    //         }
                    //     }
                    //     return false
                    // }
                    // if (isJSON(myData)) {
                    // var _data = JSON.parse(myData)
                    var _data = JSON.parse(JSON.stringify(myData))
                    var data = _data.data
                    var ulStr = ''
                    if (data.length) {
                        for (var i = 0; i < data.length; i++) {
                            (function () {
                                var str = ''
                                var iconStr = ''
                                if (data[i].isIcon) {
                                    iconStr += '<a href="javascript:;"  class="iconfont workbench-maneger">&#xe6ee;</a>'
                                }
                                if (i + 1 === data.length) {
                                    str += '<li id=' + data[i].id + ' name=' + data[i].name + ' class="no-border"><i></i><span title=' + data[i].name + '>' + data[i].name + '</span>' + iconStr + '</li>'
                                } else {
                                    str += '<li id=' + data[i].id + ' name=' + data[i].name + '><i></i><span title=' + data[i].name + '>' + data[i].name + '</span>' + iconStr + '</li>'
                                }

                                ulStr += str
                            })(i)
                        }
                    } else {
                        ulStr += ' <p class="no-data">暂无数据</p>'
                    }
                    jQuery('#my_workbench ul').html(ulStr)
                    var lis = jQuery('#my_workbench ul li')
                    for (var i = 0; i < lis.length; i++) {
                        (function () {
                            lis[i].onclick = function (e) {
                                e.stopPropagation();
                                skipWorkbench(this)
                            }
                        })(i)
                    }
                    jQuery('#my_workbench .list-wrap').css('display', 'block')
                    jQuery('#overlay_workbench').css('display', 'block')
                    // }

                }
            })
        })
    });
})(jQuery);